package com.one.consumer.user.ui;

import android.content.Intent;
import android.os.Handler;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.imagchoose.ChooseManager;
import com.one.common.manager.imagchoose.IChooseImg;
import com.one.common.manager.imageloader.ILoader;
import com.one.common.manager.imageloader.LoaderManager;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.utils.StringUtils;
import com.one.common.view.widget.HorizontalMenuItem;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseActivityC;
import com.one.consumer.R;
import com.one.consumer.user.presenter.UserPresenter;
import com.one.consumer.user.ui.view.SetUserView;
import com.one.consumer.user.ui.view.UserView;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/11/6 3:14 PM
 * description ：设置用户信息
 **/
@Route(path = RouterPath.SET_USER)
public class SetUserInfoActivity extends BaseActivityC<UserPresenter> implements SetUserView {
    @BindView(R.id.hmi_avatar)
    HorizontalMenuItem hmiAvatar;
    @BindView(R.id.hmi_name)
    HorizontalMenuItem hmiName;
    @BindView(R.id.hmi_phone)
    HorizontalMenuItem hmiPhone;

    @Override
    protected void initPresenter() {
        mPresenter = new UserPresenter(this,this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_set_user;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("个人资料");
    }

    @Override
    public void initView() {
        super.initView();
        hmiAvatar.getIvRight().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toSelectPic();
            }
        });
    }

    /**
     * 设置用户信息
     */
    @Override
    public void setUserInfo(UserInfoBean bean) {
        if (bean != null) {
            if (StringUtils.isNotBlank(bean.getMember_img())) {
                LoaderManager.getLoader().loadNet(hmiAvatar.getIvRight(), bean.getMember_img(), getOptions());
            }
            hmiName.setRightTxt(bean.getMember_nick_name());
            hmiPhone.setRightTxt(bean.getMember_account());
        }
    }

    private ILoader.Options options;

    private ILoader.Options getOptions() {
        if (options == null) {
            options = new ILoader.Options(R.mipmap.ic_avatar_default, R.mipmap.ic_avatar_default, true);
        }
        return options;
    }

    public void toSelectPic() {
        ChooseManager.getChooser().chooseSingle(this);
    }
    @OnClick(R.id.hmi_name)
    public void nameClick(){
        RouterManager.getInstance().go(RouterPath.SET_NICK_NAME);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getUserInfo();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (data != null) {
                ArrayList<String> pathList =
                        data.getStringArrayListExtra(IChooseImg.EXTRA_RESULT_SELECTION);
                if (pathList != null && pathList.size() > 0) {
                    String imgPath = pathList.get(0);

                    mPresenter.submit(imgPath, null);
                    new Handler().postDelayed(() -> mPresenter.getUserInfo(), 1000);
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void setAvatar(String url) {
        LoaderManager.getLoader().loadNet(hmiAvatar.getIvRight(), url, getOptions());
    }
}
